package com.ytjj.qmyx.mall.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.kuaidi100.sdk.contant.RedisConstants;
import com.ytjj.common.exception.ApiException;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.mall.mapper.PresellSubscribeInfoMapper;
import com.ytjj.qmyx.mall.model.PresellSubscribeInfo;
import com.ytjj.qmyx.mall.model.PresellSubscribeInfoExample;
import com.ytjj.qmyx.mall.model.dto.PresellSubscribeInfoDto;
import com.ytjj.qmyx.mall.service.PresellSubscribeInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 *
 * 
 * @description:
 */
@Service
@Slf4j
public class PresellSubscribeInfoServiceImpl implements PresellSubscribeInfoService {

    public static final String ACTIVE_NAME = "百亿补贴";

    @Autowired
    PresellSubscribeInfoMapper presellSubscribeInfoMapper;

    @Autowired
    RedisService redisService;

    @Override
    public boolean savePresell(PresellSubscribeInfo presellSubscribeInfo) {
        Map<String,Object> map = JSON.parseObject(JSON.toJSONString(presellSubscribeInfo, SerializerFeature.WriteMapNullValue));
        for (Map.Entry<String, Object> stringStringEntry : map.entrySet()) {
            if(!"id".equals(stringStringEntry.getKey()) &&
                    !"subscribeTime".equals(stringStringEntry.getKey())&&
                    !"activeName".equals(stringStringEntry.getKey())){
                if(stringStringEntry.getValue() == null || stringStringEntry.getValue() == ""){
                    throw new ApiException("必填项不能为空");
                }
            }
        }

        //是否已预约
        PresellSubscribeInfoExample presellSubscribeInfoExample = new PresellSubscribeInfoExample();
        presellSubscribeInfoExample.createCriteria().andUserIdEqualTo(presellSubscribeInfo.getUserId()).andProductMarkEqualTo(presellSubscribeInfo.getProductMark());
        long count = presellSubscribeInfoMapper.countByExample(presellSubscribeInfoExample);
        if(count > 0){
            throw new ApiException("请勿重复预约");
        }
        presellSubscribeInfo.setActiveName(ACTIVE_NAME);
        int insert = presellSubscribeInfoMapper.insert(presellSubscribeInfo);
        return insert > 0 ? true : false;
    }

    @Override
    public PresellSubscribeInfoDto getPresellInfo(Integer userId) {
        PresellSubscribeInfoDto presellSubscribeInfoDto = new PresellSubscribeInfoDto();
        //预约人数
        String s = redisService.get(RedisConstants.INITKEY);
        presellSubscribeInfoDto.setSubscribeCount(StringUtils.isNotEmpty(s) ? Long.valueOf(s) : 0);
        if(userId != null && userId != 0){
            //用户预约的信息
            PresellSubscribeInfoExample presellSubscribeInfoExample = new PresellSubscribeInfoExample();
            presellSubscribeInfoExample.createCriteria().andUserIdEqualTo(userId);
            List<PresellSubscribeInfo> presellSubscribeInfos = presellSubscribeInfoMapper.selectByExample(presellSubscribeInfoExample);
            presellSubscribeInfoDto.setPresellSubscribeInfos(presellSubscribeInfos);
        }
        return presellSubscribeInfoDto;
    }
}
